﻿using Ocr.Service.Model.Tencent;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Ocr.Service.Common
{
    public static class CommonHelper
    {

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static Int64 GetTimeStamp()
        {
            var timeStamp = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
            return Convert.ToInt64(timeStamp.TotalSeconds);
        }



        /// <summary>
        /// 获取随机字符
        /// </summary>
        /// <returns></returns>
        public static string GetNonceString()
        {
            return Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10);
        }



        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Md5(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }



        /// <summary>
        /// URL编码，将已经编码的转换为大写，不转码的保持原来的大小写
        /// </summary>
        /// <param name="value">待编码字符串</param>
        /// <param name="encoding">编码格式</param>
        /// <returns>Returns a Url encoded string</returns>
        public static string UrlEncode(string value, Encoding encoding)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < value.Length; i++)
            {
                string t = value[i].ToString();
                string k = HttpUtility.UrlEncode(t, encoding);
                if (t == k)
                {
                    stringBuilder.Append(t);
                }
                else
                {
                    stringBuilder.Append(k.ToUpper());
                }
            }
            return stringBuilder.ToString();
        }



        /// <summary>
        /// 格式化识别结果
        /// </summary>
        /// <param name="result">识别结果数据模型</param>
        /// <returns></returns>
        public static string FormatDistinguishResult(TencentDistinguishResult result)
        {
            var resultStr = string.Empty;
            try
            {
                if (result == null)
                {
                    throw new ArgumentNullException();
                }

                if (result.data != null
                    && result.data.item_list.Any())
                {
                    resultStr = string.Join("\r\n", result.data.item_list.Select(c => c.itemstring));
                }
            }
            catch (Exception exception)
            {
                resultStr = string.Empty;
            }

            return resultStr;
        }


    }
}
